package drds.configuration.db.table_mapping;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


public class SqlImpl extends Sql {
    @Override
    public String buildSqlForTableInfo(String tableName) {
        return "select * from " + tableName + " limit 0,1";
    }

    @Override
    public boolean isSupportAutoIncrementKey() {
        return true;
    }


    public void save(TableInfo tableInfo, Map<String, Object> attributeMap, StringBuilder sqlPrefix, List<Object> params) {
        sqlPrefix.append("insert into `").append(tableInfo.getTableName()).append("`(");
        StringBuilder sqlSuffix = new StringBuilder(") values(");
        for (Entry<String, Object> entry : attributeMap.entrySet()) {
            String columnName = entry.getKey();
            if (tableInfo.containsColumnName(columnName)) {
                if (params.size() > 0) {
                    sqlPrefix.append(", ");
                    sqlSuffix.append(", ");
                }
                sqlPrefix.append("`").append(columnName).append("`");
                sqlSuffix.append("?");
                params.add(entry.getValue());
            }
        }
        sqlPrefix.append(sqlSuffix.toString()).append(")");
    }


    @Override
    public void update(TableInfo tableInfo, Map<String, Object> attributeMap, Set<String> modifyFlag, String primaryKey, Object id, StringBuilder sql, List<Object> params) {
        sql.append("update `").append(tableInfo.getTableName()).append("` set ");
        for (Entry<String, Object> entry : attributeMap.entrySet()) {
            String columnName = entry.getKey();
            if (!primaryKey.equalsIgnoreCase(columnName) && modifyFlag.contains(columnName) && tableInfo.containsColumnName(columnName)) {
                if (params.size() > 0) {
                    sql.append(", ");
                }
                sql.append("`").append(columnName).append("` = ? ");
                params.add(entry.getValue());
            }
        }
        sql.append(" where `").append(primaryKey).append("` = ?");
        params.add(id);
    }


    @Override
    public String deleteById(TableInfo tableInfo) {
        String primaryKey = tableInfo.getPrimaryKey();
        StringBuilder sql = new StringBuilder(60);
        sql.append("delete from `");
        sql.append(tableInfo.getTableName());
        sql.append("` where `").append(primaryKey).append("` = ?");
        return sql.toString();
    }


    @Override
    public String findById(TableInfo tableInfo, String columns) {
        StringBuilder sql = new StringBuilder("select ");
        if (columns.trim().equals("*")) {
            sql.append(columns);
        } else {
            String[] columnsArray = columns.split(",");
            for (int i = 0; i < columnsArray.length; i++) {
                if (i > 0) {
                    sql.append(", ");
                }
                sql.append("`");
                sql.append(columnsArray[i].trim());
                sql.append("`");
            }
        }
        sql.append(" from `");
        sql.append(tableInfo.getTableName());
        sql.append("` where `").append(tableInfo.getPrimaryKey()).append("` = ?");
        return sql.toString();
    }


}
